﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccaneer.Properties;
using System.Drawing;
using System.IO;
using System.Threading;

namespace Buccaneer
{
    public enum SelectPosition { left, selected, right };
    
    class TabInitializer
    {
        public static BitmapButton[] tabs = null;
        public static PictureBox[] tabSeparators = null;
        public static Bitmap tabSelected, tabUnselectedRight, tabUnselectedLeft, tabSeparatorLeft, tabSeparatorRight, tabEndRight;

        public static void Initialize()
        {
            try
            {
                tabSelected = new Bitmap(Directory.GetCurrentDirectory() + @"\Layout\tabSelected.png");
                tabUnselectedRight = new Bitmap(Directory.GetCurrentDirectory() + @"\Layout\tabUnselectedRight.png");
                tabUnselectedLeft = new Bitmap(Directory.GetCurrentDirectory() + @"\Layout\tabUnselectedLeft.png");
                tabSeparatorLeft = new Bitmap(Directory.GetCurrentDirectory() + @"\Layout\tabSeparatorLeft.png");
                tabSeparatorRight = new Bitmap(Directory.GetCurrentDirectory() + @"\Layout\tabSeparatorRight.png");
                tabEndRight = new Bitmap(Directory.GetCurrentDirectory() + @"\Layout\tabEndRight.png");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in reading a tab layout image.\n\nException info: " + ex.Message);
                Thread.CurrentThread.Abort();
                return;
            }
        }

        public static BitmapButton AddNewTab(string name)
        {
            try
            {
                Array.Resize<BitmapButton>(ref tabs, tabs.Length + 1);
            }
            catch (NullReferenceException)
            {
                tabs = new BitmapButton[] { new BitmapButton() };
            }

            tabs[tabs.Length - 1] = NewTab(name, SelectPosition.selected, tabs.Length - 1);

            return tabs[tabs.Length - 1];
        }

        private static BitmapButton NewTab(string text, SelectPosition selected, int tabIndex)
        {
            BitmapButton newTab;

            Image img = BitmapHandler.resizeImage(selected == SelectPosition.selected ? tabSelected : (selected == SelectPosition.right ? tabUnselectedRight : tabUnselectedLeft), new Size((590 / tabs.Length - tabs.Length * tabSeparatorLeft.Width) < tabSelected.Width ? (590 / tabs.Length - tabs.Length * tabSeparatorLeft.Width) : tabSelected.Width, tabSelected.Height));

            Graphics g = Graphics.FromImage(img);

            StringFormat strFormat = new StringFormat();
            strFormat.Alignment = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(text, new Font("Tahoma", 10), Brushes.Black,
                new RectangleF(new Point(0,0), img.Size), strFormat);

            newTab = new BitmapButton();
            newTab.Left =
                136 + (((590 / tabs.Length) * tabIndex) < (tabSeparatorLeft.Width + tabSelected.Width) 
                    ? ((590 / tabs.Length) * tabIndex) 
                    : (tabSeparatorLeft.Width + tabSelected.Width) * tabIndex);
            newTab.Top = 0;
            newTab.Image = img;
            newTab.Size = img.Size;
            newTab.TabIndex = 0;
            newTab.Text = text;
            newTab.FlatStyle = FlatStyle.Standard;

            return newTab;
        }

        private static PictureBox NewSeparator(int separatorNr, int selectedTab)
        {
            PictureBox imgPhoto = new PictureBox();
            imgPhoto.BorderStyle = BorderStyle.None;
            imgPhoto.Size = separatorNr == tabSeparators.Length - 1 ? tabEndRight.Size : tabSeparatorLeft.Size;
            imgPhoto.Left =
                136 + (((590 / tabs.Length) * separatorNr) < (tabSeparatorLeft.Width + tabSelected.Width)
                    ? ((590 / tabs.Length) * separatorNr)
                    : (tabSeparatorLeft.Width + tabSelected.Width) * separatorNr) + 
                    ((590 / tabs.Length - tabs.Length * tabSeparatorLeft.Width) < tabSelected.Width 
                        ? (590 / tabs.Length - tabs.Length * tabSeparatorLeft.Width) 
                        : tabSelected.Width);
            imgPhoto.Top = 0;
            imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
            imgPhoto.Image = separatorNr == tabSeparators.Length - 1 ? tabEndRight : (selectedTab > separatorNr ? tabSeparatorLeft : tabSeparatorRight);

            return imgPhoto;
        }

        public static void DeleteTab(string name)
        {
            bool deleted = false;

            for (int i = 0; i < tabs.Length; i++)
            {
                if (!deleted)
                {
                    deleted = tabs[i].Text == name;
                }
                else
                {
                    tabs[i - 1] = tabs[i];
                }
            }

            Array.Resize<BitmapButton>(ref tabs, tabs.Length - 1);
        }

        public static BitmapButton[] UpdateTabs(int selectedTab)
        {
            for (int i = 0; i < tabs.Length; i++)
            {
                tabs[i] = NewTab(tabs[i].Text, i == selectedTab ? SelectPosition.selected : (i < selectedTab ? SelectPosition.left : SelectPosition.right), i);
            }

            tabSeparators = new PictureBox[tabs.Length];

            for (int i = 0; i < tabSeparators.Length; i++)
            {
                tabSeparators[i] = NewSeparator(i, selectedTab);
            }

            return tabs;
        }
    }
}
